import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import {message} from 'antd'

const useAuth=()=>{
    const navigate=useNavigate()
    const [isAuthenticated,setIsAuthenticated]=useState(()=>{
        const token = localStorage.getItem('token')
        return !!token
    })
    useEffect(()=>{
        const token = localStorage.getItem('token')
        if(!token){
            setIsAuthenticated(false)
            message.error('请先登录')
            setTimeout(()=>{
                navigate('/login')
            },1000)
        }
    },[navigate])
return isAuthenticated
}
export default useAuth

